﻿using System; 
using System.Data;
using System.Collections.Generic; 
using JOIN.Rights.Model;
using Kevin.Framework.Data;
using JOIN.Rights.BLL.Cache;

/*------------------------------------------------------------------------
   苏州捷安信息科技有限公司 版权所有			
   文件名: TU_Project.cs
   作 者 : 席吉波
   日 期 : 2012-10-19
   备 注 : 当您对本文件更改时请写明修改人、修改日期以及事由
  ------------------------------------------------------------------------
*/

namespace JOIN.Rights.BLL
{
	 	//TU_Project
		/// <summary>
	/// Project 业务操作类
	/// </summary>
	public partial class ProjectBLL:BLLBase<ProjectInfo>,ISelect<ProjectInfo>
	{
		#region   构造函数
		
		/// <summary>
		/// ProjectBLL  构造函数  
		/// </summary>
		public ProjectBLL(ProjectInfo entity)
			:base(entity)
		{
		
		}
		/// <summary>
		/// ProjectBLL  构造函数  
		/// </summary>
		/// <param name="operationMode">操作模式</param>
        /// <param name="jsonEntity">前台javascript传过来的JSON实体</param>
		public ProjectBLL(string operationMode, string jsonEntity)
        {
            OperState cmd = (OperState)Enum.Parse(typeof(OperState), operationMode);
            ProjectInfo entity = ConvertJSON2Entity(jsonEntity);
            base.OperState = cmd;
            base.Entity = entity;
            base.ServiceResult = new ServiceResult<ProjectInfo>();
            base.ServiceResult.Successed = true;
            base.ServiceResult.Object = base.Entity;
        }

        
        
        #endregion
        
        #region 查询方法
        
        /// <summary>
        /// 获取 ProjectInfo 集合
        /// </summary>
        /// <param name="pageSize">分页记录数</param>
        /// <param name="pageIndex">页码索引</param>
        /// <returns></returns>
        public ServiceResult<ProjectInfo> SelectByProperties(int pageSize, int pageIndex)
        {
            this.ServiceResult = new ServiceResult<ProjectInfo>();
            try
            {
                int recordCount =0;
                List<ProjectInfo> list = base.Entity.SelectedByProperties<ProjectInfo>(out recordCount,pageSize,pageIndex);
                
                ServiceResult.Successed = true;
                ServiceResult.RecordCount = recordCount;
                ServiceResult.List = list;
                
            }
            catch(Exception ex){
                ServiceResult.ErrorMsg = "数据查询失败:"+ex.Message;
                ServiceResult.Successed = false;
                ServiceResult.RecordCount = 0;
                ServiceResult.List = null;
            }

            return ServiceResult;
        }

        public DataTable SelectAllProjects()
        {
          DataTable result = base.Entity.SelectedByProperties();
          return result;
        }
        /// <summary>
        /// 加载项目信息[使用缓存]
        /// </summary>
        /// <returns></returns>
        public List<CodeNameItem> LoadCodeNameFromCache()
        {
            List<CodeNameItem> list = null;
            if (!CodeNameCache.Instance.TryGetValue("projects", out list))
            {
                //ProjectBLL bll = new ProjectBLL(new ProjectInfo());
                DataTable dt = SelectAllProjects();
                list = new List<CodeNameItem>();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new CodeNameItem(dr["ProjectID"].ToString(), dr["ProjectName"].ToString()));
                }
                CodeNameCache.Instance.Add("projects", list);
            }
            return list;
        }
        
        #endregion
        
        #region 重写基类方法

        protected override bool PreSaveProcess()
        {
            if (OperState == BLL.OperState.Add)
            {
                base.Entity.ID = Guid.NewGuid().ToString();
            }
            return base.PreSaveProcess();
        }

        protected override void AfterSaveProcess()
        {
           this.LoadCodeNameFromCache().Add(new CodeNameItem ( base.Entity.ProjectID, base.Entity.ProjectName ));
        }
        #endregion
   
	}
}